#!/usr/bin/env python3
# encoding=utf-8

import datetime

from app.db.models.tasks import Task
from app.db.models.wass import ScanEvents
from app.libs.ipv6_check import IPv6Score

class FixIPv6Score:

    def service(self):
        print(f"{datetime.datetime.now()} 开始同步......")
        tasks = Task.objects.filter(taskType='ipv6', status='completed').only('result.addition', 'id', 'taskSessionId')
        for task in tasks:
            task_session_id = task.taskSessionId
            addition = task.result.addition
            # if addition.get("score") is None:
            event = ScanEvents.objects.filter(task_session_id=task_session_id).first()
            event_detail = event.detail if event else {}
            score_obj = IPv6Score(event_detail)
            score = score_obj.total_score
            addition['score'] = score
            Task.objects(pk=task.id).update(result__addition=addition)
        print(f"{datetime.datetime.now()} 同步完成")
